/**
 * @description 手机号校验格式
 * @author bing
 * @date 2022-01-28 17:01
 * @param {*} rule
 * @param {*} value
 * @param {function} callback
 */
const validatePhone = (rule, value, callback) => {
  if (value === "" || value == undefined) {
    callback();
  } else {
    if (!/^1[3456789]\d{9}$/.test(value)) {
      callback(new Error($i18n.t("mesUserExtend.inputMobileCorrect")));
    } else {
      callback();
    }
  }
};
/**
 * @description 邮箱校验格式
 * @author bing
 * @date 2022-01-28 17:05
 * @param {*} rule
 * @param {*} value
 * @param {function} callback
 */
const validateEmail = (rule, value, callback) => {
  if (value === "" || value == undefined) {
    callback();
  } else {
    if (!/[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+/.test(value)) {
      callback(new Error($i18n.t("mesUserExtend.inputEmailCorrect")));
    } else {
      callback();
    }
  }
};
import {
  exportUrl,
  getUserCodeList,
  getLineCodeList,
  getTeamCodeList,
  getWorkCellCodeList,
  getEducationList,
  getSkillsCertificationList,
  getWorkTypeList,
  getEmployeeTypeList,
  getDepartmentList,
  getUserTypeList,
  getPostCodeList,
  getPositionCodeList,
} from "@/api/employee/mesUserExtend";
import { deleteFile } from "@/api/common";
import store from "@/store";
import url from "@/axios/url";
import { checkStr } from "@/utils/judge";
const { adminUrl } = url();
// 列表头按钮配置
export const operateList = [
  {
    name: $i18n.t("common.export"),
    type: "primary",
    icon: "el-icon-download",
    handleName: "export", // 点击时返回的标识
    url: exportUrl,
    // auth: "auth:operlog:export", //权限字段
  },
  // {
  //   name: '导入',
  //   type: 'primary',
  //   icon: 'el-icon-upload',
  //   handleName: 'uploadFile', //点击时返回的标识
  // },
  {
    name: $i18n.t("common.add"),
    type: "primary",
    icon: "el-icon-circle-plus-outline",
    handleName: "add", // 点击时返回的标识
  },
  {
    name: $i18n.t("common.delete"),
    type: "danger",
    icon: "el-icon-delete",
    handleName: "deleteBatch", // 点击时返回的标识
    isMultiple: true,
  },
];
// 行末按钮配置
export const rowConfig = [
  {
    name: $i18n.t("common.edit"),
    type: "primary",
    class: "row-btn-primary",
    handleName: "edit", // 点击时返回的标识
  },
  {
    name: $i18n.t("common.details"),
    type: "info",
    class: "row-btn-info",
    handleName: "detail", // 点击时返回的标识
  },
  {
    name: $i18n.t("common.delete"),
    type: "danger",
    class: "row-btn-danger",
    handleName: "delete", // 点击时返回的标识
  },
];
// 搜索条件配置
export const searchConfig = {
  data() {
    const self = this;
    return {
      searchDialog: {
        inline: true,
        formDesc: {
          userCode: {
            type: "input",
            label: $i18n.t("mesUserExtend.userCode"),
            // type: 'select',
            // prop: {
            //   value: 'value',
            //   text: 'description',
            // },
            // options: async (data) => {
            //   const list = await getUserCodeList()
            //   return list.data.result
            // },
          },
          userName: {
            type: "input",
            label: $i18n.t("mesUserExtend.userName"),
          },
          workTypeCode: {
            type: "select",
            label: '工种',
            attrs: {
              filterable: true,
            },
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getWorkTypeList();
              return list.data.result;
            },
          },
          skillsCertificationCode: {
            type: "select",
            label: '技能认证',
            attrs: {
              filterable: true,
            },
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getSkillsCertificationList();
              return list.data.result;
            },
          },
          employeeTypeCode: {
            type: "select",
            label: '是否为外包人员',
            attrs: {
              filterable: true,
            },
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getEmployeeTypeList();
              return list.data.result;
            }
          },
          // workshopCode: {
          //   type: "select",
          //   label: $i18n.t("common.workshopName"),
          //   attrs: {
          //     filterable: true,
          //   },
          //   prop: {
          //     value: "modelingCode",
          //     text: "modelingName",
          //   },
          //   options: async (data) => {
          //     const params = {
          //       modelingCode: this.$store.getters.factoryCode,
          //       level: 4,
          //       modelingType: "0",
          //     };
          //     const list = await getLineCodeList(params);
          //     return list.data.result;
          //   },
          // },
          // lineCode: {
          //   type: "select",
          //   label: $i18n.t("common.line"),
          //   optionsLinkageFields: ["workshopCode"],
          //   isReloadOptions: true,
          //   attrs: {
          //     filterable: true,
          //   },
          //   prop: {
          //     value: "modelingCode",
          //     text: "modelingName",
          //   },
          //   options: async (data) => {
          //     if (data.workshopCode) {
          //       const list = await getLineCodeList({
          //         modelingCode: data.workshopCode,
          //       });
          //       return list.data.result;
          //     } else {
          //       return [];
          //     }
          //     // const params = {
          //     //   modelingCode: this.$store.getters.factoryCode,
          //     //   level: 5,
          //     // };
          //     // const list = await getLineCodeList(params);
          //     // return list.data.result;
          //   },
          // },
          // workCellCode: {
          //   type: "select",
          //   label: $i18n.t("common.workCell"),
          //   optionsLinkageFields: ["lineCode"],
          //   isReloadOptions: true,
          //   attrs: {
          //     filterable: true,
          //   },
          //   prop: {
          //     value: "modelingCode",
          //     text: "modelingName",
          //   },
          //   options: async (data) => {
          //     if (data.lineCode) {
          //       const list = await getWorkCellCodeList({
          //         modelingCode: data.lineCode,
          //       });
          //       return list.data.result;
          //     } else {
          //       return [];
          //     }
          //   },
          // },
          // teamCode: {
          //   type: "select",
          //   label: $i18n.t("common.team"),
          //   optionsLinkageFields: ["lineCode"],
          //   isReloadOptions: true,
          //   attrs: {
          //     filterable: true,
          //   },
          //   prop: {
          //     value: "teamCode",
          //     text: "teamName",
          //   },
          //   options: async (data) => {
          //     // const list = await getTeamCodeList();
          //     // return list.data.result;
          //     if (data.lineCode) {
          //       const list = await getTeamCodeList({ lineCode: data.lineCode });
          //       return list.data.result;
          //     } else {
          //       return [];
          //     }
          //   },
          // },
          // workCellName: {
          //   type: 'input',
          //   label: '工位名称',
          // },
        },
      },
    };
  },
};
// 新增/删除表单配置
export const formDescDialogMixin = {
  data() {
    // var validatePassPhone = (rule, value, callback) => {
    //   setTimeout(() => {
    //     if (!checkStr(value, 'phone')) {
    //       callback(new Error('电话格式有误！'))
    //     } else {
    //       callback()
    //     }
    //   }, 100)
    // }
    // var validatePassEmail = (rule, value, callback) => {
    //   setTimeout(() => {
    //     if (!checkStr(value, 'email')) {
    //       callback(new Error('邮箱格式有误！'))
    //     } else {
    //       callback()
    //     }
    //   }, 100)
    // }
    const self = this;
    return {
      formDescDialog: {
        span: 24,
        // disabled: false,
        // isShowSubmitBtn: true,
        // rules: {
        //   email: [{ validator: validatePassEmail, trigger: 'blur' }],
        //   phone: [{ validator: validatePassPhone, trigger: 'blur' }]
        // },
        formDesc: {
          userCode: {
            type: "input",
            label: $i18n.t("mesUserExtend.userCode"),
            layout: 12,
            required: true,
          },
          userName: {
            type: "input",
            label: $i18n.t("mesUserExtend.userName"),
            layout: 12,
            required: false,
            disabled: true,
          },
          //
          departmentCode: {
            type: "select",
            // label: $i18n.t("mesUserExtend.education"),
            label: '部门名称',
            layout: 12,
            required: false,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getDepartmentList();
              return list.data.result;
            },
          },
          workTypeCode: {
            type: "tree-select",
            label: '工种',
            layout: 12,
            required: true,
            attrs: {
              "value-consists-of": "ALL_WITH_INDETERMINATE",
              limit: 2,
              multiple: true,
              normalizer(node) {
                return {
                  id: node.value,
                  label: node.description,
                  children: node.children,
                };
              },
            },
            options: async (data) => {
              const list = await getWorkTypeList();
              return list.data.result;
            },
          },
          skillsCertificationCode: {
            type: "tree-select",
            label: '技能认证',
            layout: 12,
            required: true,
            attrs: {
              "value-consists-of": "ALL_WITH_INDETERMINATE",
              limit: 1,
              multiple: true,
              normalizer(node) {
                return {
                  id: node.value,
                  label: node.description,
                  children: node.children,
                };
              },
            },
            options: async (data) => {
              const list = await getSkillsCertificationList();
              return list.data.result;
            },
          },
          employeeTypeCode: {
            type: "select",
            // label: $i18n.t("mesUserExtend.education"),
            label: '是否为外包人员',
            layout: 12,
            required: false,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getEmployeeTypeList();
              return list.data.result;
            },
          },
          //
          workshopCode: {
            type: "select",
            label: $i18n.t("common.workshopName"),
            layout: 12,
            required: false,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              const params = {
                modelingCode: this.$store.getters.factoryCode,
                level: 4,
                modelingType: "0",
              };
              const list = await getLineCodeList(params);
              this.submitProdAreaList = list.data.result;
              return list.data.result;
            },
          },
          lineCode: {
            type: "select",
            label: $i18n.t("common.line"),
            isOptions: true,
            optionsLinkageFields: ["workshopCode"],
            layout: 12,
            required: false,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              this.submitProdLineList = [];
              if (!data.workshopCode) return [];
              const res = await getWorkCellCodeList({
                modelingCode: data.workshopCode,
              });
              const { result } = res.data;
              this.submitProdLineList = result || [];
              return this.submitProdLineList;

              // const params = {
              //   modelingCode: this.$store.getters.factoryCode,
              //   level: 5,
              // };
              // const list = await getLineCodeList(params);
              // this.submitProdLineList = list.data.result;
              // return list.data.result;
            },
          },
          workCellCode: {
            type: "select",
            label: $i18n.t("common.workCell"),
            isOptions: true,
            optionsLinkageFields: ["lineCode"],
            attrs: {
              filterable: true,
            },
            layout: 12,
            required: false,
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              this.submitWorkCellList = [];
              if (!data.lineCode) return [];
              const res = await getWorkCellCodeList({
                modelingCode: data.lineCode,
              });
              const { result } = res.data;
              this.submitWorkCellList = result || [];
              return this.submitWorkCellList;
            },
          },
          teamCode: {
            type: "select",
            label: $i18n.t("common.team"),
            layout: 12,
            required: false,
            optionsLinkageFields: ["lineCode"],
            attrs: {
              filterable: true,
            },
            prop: {
              value: "teamCode",
              text: "teamName",
            },
            options: async (data) => {
              this.submitTeamList = [];
              if (data.lineCode) {
                const list = await getTeamCodeList({ lineCode: data.lineCode });
                this.submitTeamList = list.data.result || [];
                return list.data.result;
              } else {
                return [];
              }
            },
          },
          // workCellName: {
          //   type: 'input',
          //   label: '工位名称',
          //   layout: 12,
          //   required: false,
          // },
          education: {
            type: "select",
            label: $i18n.t("mesUserExtend.education"),
            layout: 12,
            required: false,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getEducationList();
              return list.data.result;
            },
          },
          birthday: {
            type: "date",
            label: $i18n.t("mesUserExtend.birthday"),
            layout: 12,
            required: false,
            attrs: {
              valueFormat: "yyyy-MM-dd",
            },
          },
          timeEntry: {
            type: "date",
            label: $i18n.t("mesUserExtend.timeEntry"),
            layout: 12,
            required: false,
            attrs: {
              valueFormat: "yyyy-MM-dd",
            },
          },
          departureTime: {
            type: "date",
            label: $i18n.t("mesUserExtend.departureTime"),
            layout: 12,
            required: false,
            attrs: {
              valueFormat: "yyyy-MM-dd",
            },
          },
          userType: {
            type: "select",
            label: $i18n.t("mesUserExtend.userType"),
            layout: 12,
            required: false,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getUserTypeList();
              return list.data.result;
            },
          },
          //岗位从platform用户直接带过来
          // postCode: {
          //   type: 'select',
          //   label: $i18n.t('mesUserExtend.postCode'),
          //   layout: 12,
          //   required: false,
          //   prop: {
          //     value: 'postCode',
          //     text: 'postName'
          //   },
          //   options: async (data) => {
          //     const list = await getPostCodeList({'status':'1'})
          //     this.submitPostList = list.data.result

          //     return list.data.result
          //   },
          //   on: {
          //     change: (val) => {
          //       const findItem = this.submitPostList.find((item) => item.postCode == val) || {}
          //       self.$set(self.formDataDialog, 'postName', findItem.postName || '')
          //     }
          //   }
          // },
          email: {
            type: "input",
            label: $i18n.t("mesUserExtend.email"),
            layout: 12,
            required: false,
            rules: [
              { validator: validateEmail, trigger: ["change", "blur"] },
              { max: 50, message: $i18n.t("common.overlongPrompt", { length: 50 }), trigger: ["change", "blur"] }
            ]
          },
          phone: {
            type: "input",
            label: $i18n.t("mesUserExtend.phone"),
            layout: 12,
            required: false,
            rules: [
              { validator: validatePhone, trigger: ["change", "blur"] },
              { max: 20, message: $i18n.t("common.overlongPrompt", { length: 20 }), trigger: ["change", "blur"] }
            ],
          },
          // positionCode: {
          //   type: 'select',
          //   label: $i18n.t('mesUserExtend.position'),
          //   layout: 12,
          //   required: false,
          //   prop: {
          //     value: 'value',
          //     text: 'description'
          //   },
          //   options: async (data) => {
          //     const list = await getPositionCodeList()
          //     this.submitPositionList = list.data.result
          //     return list.data.result
          //   },
          //   on: {
          //     change: (val) => {
          //       const findItem = this.submitPositionList.find((item) => item.value == val) || {}
          //       self.$set(self.formDataDialog, 'positionName', findItem.description || '')
          //     }
          //   }
          // },
          userFace: {
            type: "image-uploader",
            label: $i18n.t("mesUserExtend.userAvatar"),
            layout: 12,
            required: false,
            attrs: {
              fileSize: 3, // 文件大小限制 单位M
              // fileType: ["png"],
              multiple: true, // 是否支持多选
              // disabled: true,
              size: 80, // 图片显示大小
              action: `${adminUrl}file`,
              data: {
                bucket: "messervice",
              },
              headers: {
                Satoken: store.getters.token,
              },
              beforeRemove: (data, list) => {
                const bucket = "messervice";
                const filename = data.title;

                return new Promise((resolve, reject) => {
                  deleteFile(`${adminUrl}file`, {
                    bucket,
                    filename,
                  })
                    .then((res) => {
                      resolve(res.data);
                    })
                    .catch((error) => {
                      reject(error);
                    });
                });
              },
              responseFn(response, file) {
                return {
                  src: response.result.path,
                  // title: response.result.object,
                  ...response.result,
                };
              },
            },
          },
          // factoryCode: {
          //   type: 'input',
          //   label: '工厂编码',
          //   layout: 12,
          //   required: false,
          // },
          remark: {
            type: "textarea",
            label: $i18n.t("common.remark"),
            layout: 12,
            required: false,
            rules: [{ max: 200, message: $i18n.t("common.overlongPrompt", { length: 200 }), trigger: ["change", "blur"] }]
          },
        },
      },
    };
  },
};
